package com.xbb.kafka.consumer;

import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;

import java.util.Arrays;
import java.util.Properties;

public class MyConsumer {
  public static void main(String[] args) {
    Properties properties = new Properties();

    properties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "hadoop1:9092");
    properties.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, true);
    properties.put(ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG, "1000");
    properties.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringDeserializer");
    properties.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringDeserializer");

    properties.put(ConsumerConfig.GROUP_ID_CONFIG, "bigdata");

    KafkaConsumer<String, String> consumer = new KafkaConsumer<>(properties);
    // 订阅
    consumer.subscribe(Arrays.asList("top", "bottom"));

    while (true) {
      ConsumerRecords<String, String> poll = consumer.poll(100);
      for (ConsumerRecord<String, String> record : poll) {
        System.out.println(record.key() + "--" + record.offset());
      }
    }

  }
}
